//
//  HJDGestationViewController.m
//  JYHospital
//
//  Created by 胡金东 on 16/5/13.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDGestationViewController.h"
#import "HJDFileViewController.h"
#import "AFNetworking.h"


@interface HJDGestationViewController ()
{
    int status;
}

@property (nonatomic,strong) UITextField *phoneText;

@property (nonatomic,strong) UITextField *mesTestText;

@property (nonatomic,assign) int  second;
@property (nonatomic,strong) UIButton *btn;
@property (nonatomic,strong) NSTimer * timer;


@end

@implementation HJDGestationViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"电子病历";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor getColorWithHexNumber:@"848484"]}];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(backBtnClick) image:@"︿" HighImage:nil];

    self.second = 60;
     [self setUpText];
   
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
   
    
}

//隐藏键盘
-(void)viewTapped:(UITapGestureRecognizer*)tap
{
    
    [self.view endEditing:YES];
    
}

//返回
-(void)backBtnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)setUpText
{
    CGFloat lblX = HJDHorizontalMargin;
    CGFloat lblY = HJDNavigHeight + HJDVerticalMargin +HJDVerticalMargin/2;
    CGFloat lblW = 300;
    CGFloat lblH = HJDVerticalMargin;
    
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(lblX, lblY, lblW, lblH)];
    lbl.text = @"请和您的档案手机信息一致";
    lbl.textColor = [UIColor getColorWithHexNumber:@"5CC6AC"];
    lbl.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:lbl];
    
    CGFloat phoneTextX = lblX;
    CGFloat phoneTextY = CGRectGetMaxY(lbl.frame)+HJDVerticalMargin;
    CGFloat phoneTextW = HJDWidth - HJDHorizontalMargin*2;
    CGFloat phoneTextH = 50;
    
    self.phoneText = [[UITextField alloc]initWithFrame:CGRectMake(phoneTextX, phoneTextY, phoneTextW, phoneTextH)];
    self.phoneText.clearButtonMode = UITextFieldViewModeAlways;
    self.phoneText.placeholder = @"请输入手机号";
    [self.phoneText setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    [self.phoneText setValue:[UIColor getColorWithHexNumber:@"CFCFCF"] forKeyPath:@"_placeholderLabel.textColor"];
    self.phoneText.keyboardType = UIKeyboardTypeNumberPad;
    [self.phoneText setRadius:10 borderWith:1 borderColor:[UIColor getColorWithHexNumber:@"F1F1EE"]];
    
    self.phoneText.leftView = [[UIView alloc]initWithFrame:CGRectMake(5, 5, 25,50 )];
    self.phoneText.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 37/2, 58/2)];
    image.image = [UIImage imageNamed:@"档案手机"];
    [self.phoneText.leftView addSubview:image];

    [self.view addSubview:self.phoneText];
    
    
    CGFloat mesTestTextX = phoneTextX;
    CGFloat mesTestTextY = CGRectGetMaxY(self.phoneText.frame)+HJDVerticalMargin;
    CGFloat mesTestTextW = phoneTextW - 120;
    CGFloat mesTestTextH = 50;
    
    self.mesTestText = [[UITextField alloc]initWithFrame:CGRectMake(mesTestTextX, mesTestTextY, mesTestTextW, mesTestTextH)];
    self.mesTestText.placeholder = @" 输入验证码";
    [self.mesTestText setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    [self.mesTestText setValue:[UIColor getColorWithHexNumber:@"CFCFCF"] forKeyPath:@"_placeholderLabel.textColor"];
    self.mesTestText.keyboardType = UIKeyboardTypeNumberPad;
    [self.mesTestText setRadius:10 borderWith:1 borderColor:[UIColor getColorWithHexNumber:@"F1F1EE"]];
    [self.view addSubview:self.mesTestText];
    
    CGFloat btnX = CGRectGetMaxX(self.mesTestText.frame)+5;
    CGFloat btnY = mesTestTextY;
    CGFloat btnW = 115;
    CGFloat btnH = mesTestTextH;
    
    self.btn = [[UIButton alloc]initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
    [self.btn setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.btn setRadius:10 borderWith:0 borderColor:nil];
    [self.btn setBackgroundColor:[UIColor getColorWithHexNumber:@"FF9EAC"]];
    [self.btn addTarget:self action:@selector(BtnClickReg) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
    
    
    CGFloat seebtnX = HJDHorizontalMargin;
    CGFloat seebtnY = CGRectGetMaxY(self.btn.frame) + HJDVerticalMargin * 2;
    CGFloat seebtnW = phoneTextW;
    CGFloat seebtnH = 60;
    
    UIButton *seebtn = [[UIButton alloc]initWithFrame:CGRectMake(seebtnX, seebtnY, seebtnW, seebtnH)];
    [seebtn setTitle:@"查看档案" forState:UIControlStateNormal];
//    要失效
//    seebtn.font = [UIFont systemFontOfSize:19];
    seebtn.titleLabel.font = [UIFont systemFontOfSize:19];
    [seebtn setRadius:10 borderWith:0 borderColor:nil];
    [seebtn setBackgroundColor:[UIColor getColorWithHexNumber:@"5CC6AC"]];
    [seebtn addTarget:self action:@selector(seebtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:seebtn];
    

}

-(void)BtnClickReg{
    
    NSString *phone = @"^1+[34578]+\\d{9}";
    NSPredicate *pr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phone];
    if ([pr evaluateWithObject:self.phoneText.text]) {
        
            NSString *url = @"/app/smsSend.do";
            NSString *path = [Hurl stringByAppendingString:url];
            
            AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            params[@"phone"] = self.phoneText.text;
            params[@"type"] = @"false";
            [mgr POST:path parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSLog(@"res%@",responseObject);
                NSDictionary *dict = (NSDictionary *)responseObject;
                NSString *resultCode = dict[@"resultCode"];
                if ([resultCode integerValue]==0) {
                    [MBProgressHUD showSuccess:@"验证码已发送"];
                    [self sendVerificationCode];
                }
                if ([resultCode integerValue] == 006) {
                    [MBProgressHUD showError:@"账号不存在,请重新输入"];
                }
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//                NSLog(@"error%@",error);
                [MBProgressHUD showError:@"网络问题，发送失败"];
            }];
            
           }else{
        [MBProgressHUD showError:@"手机号不正确,请重新输入"];
    }
}


-(void)seebtnClick
{
    if (self.mesTestText.text.length >0) {
        if (self.mesTestText.text.length ==6) {
//
    
  
    NSString *url = @"/app/fileCheck.do";
    NSString *path = [Hurl stringByAppendingString:url];
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = self.phoneText.text;
    params[@"code"] = self.mesTestText.text;
    [mgr POST:path parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSString *resultCode = dict[@"resultCode"];
        if ([resultCode integerValue]==0) {
            HJDFileViewController *file = [[HJDFileViewController alloc]init];
            [self.navigationController pushViewController:file animated:YES];
            NSString *statusCase = @"1";
            NSUserDefaults *defaSC = [NSUserDefaults standardUserDefaults];
            [defaSC setObject:statusCase forKey:@"stusCase"];

            
        }else if ([resultCode integerValue]==005){
            [MBProgressHUD showError:@"验证码失效或错误"];}

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
      
       [MBProgressHUD showError:@"服务器升级，请稍后再试"];
    }];
        }else {
            [MBProgressHUD showError:@"验证码不对"];
        }
    }else{
        [MBProgressHUD showError:@"验证码不可为空"];
    }

    
//    HJDFileViewController *file = [[HJDFileViewController alloc]init];
//    [self.navigationController pushViewController:file animated:YES];
//    

}


-(void)changeMin
{
    if (self.second == 0) {
        
        [self.btn setTitle:@"重新获取验证码" forState:UIControlStateNormal];
        self.btn.enabled = YES;
        self.btn.hidden = NO;
        [self.timer invalidate];
        self.second = 60;
        return;
    }
    
    self.btn.enabled = NO;
    [self.btn setTitle:[NSString stringWithFormat:@"%d s",self.second] forState:UIControlStateNormal];
    self.second --;
    
}
- (void)sendVerificationCode
{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeMin)  userInfo:nil repeats:YES];
}


@end
